<template>
<div class="login-wrap">
    <el-form class="login-form" label-position="top" label-width="80px" :model="User">
        <h2>用户登录</h2>
        <el-form-item label="用户名">
            <el-input v-model="User.username"></el-input>
        </el-form-item>
        <el-form-item label="密码">
            <el-input v-model="User.password" type="password"></el-input>
        </el-form-item>
        <el-button class="login-btn" type="primary" @click.prevent="handleLogin">登录</el-button>
    </el-form>
</div>
</template>

<script>
export default {
    data() {
        return {
            User: {
                username: '',
                password: ''
            }
        }
    },
    methods: {
        handleLogin() {
            // 无后端时暂时使用
            // if (this.User.username === 'admin' && this.User.password === 'admin') {
            //     // 0. 保存token值;这里模拟假的token
            //     sessionStorage.setItem('token',this.User.username)
            //     this.$router.push({name:'home'})
            //     this.$message.success('登录成功')
            // } else {
            //     this.$message.error('登录失败')
            // }
            this.$http.post('login', this.User).then((res) => {
                const {
                    data,
                    msg,
                    code
                } = res.data
                //1 跳转 2 提示 / 1 提示
                if (code === 200) {
                    sessionStorage.setItem('username', this.User.username)
                    this.$router.push({
                        name: 'home'
                    })
                    this.$message.success(msg)
                } else {
                    this.$message.error(msg)
                }

            }).catch((e)=>{
                this.$message.error('请检查网络或者校园vpn连接情况')
            })
        }
    }
}
</script>

<style>
.login-wrap {
    height: 100%;
    background-color: #324152;
    /* Flex弹性盒布局 */
    display: flex;
    justify-content: center;
    align-items: center;
}

.login-wrap .login-form {
    width: 400px;
    background-color: #fff;
    border-radius: 5px;
    padding: 30px;
}

.login-wrap .login-btn {
    width: 100%;
}
</style>
